<?php
namespace Home\Model;
use Think\Model;
/**
* Article
*/
class ArticleModel extends Model
{
	/**
	 * 自动验证
	 * self::EXISTS_VALIDATE 或者0 存在字段就验证（默认）
	 * self::MUST_VALIDATE 或者1 必须验证
	 * self::VALUE_VALIDATE或者2 值不为空的时候验证
	 */
	protected $_validate = array(
		array('title', 'require', '标题不能为空'), //默认情况下用正则进行验证
		array('author', 'require', '标题不能为空'),
		array('profiles', 'require', '标题不能为空'),
		array('content', 'require', '标题不能为空'),
	);
	/**
	 * 文章首图上传
	 * @return [type] [description]
	 */
	public function uploadImg()
	{
		$upload = new \Think\Upload();
		$upload->maxSize = 2097152 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './App/artImg/'; // 设置附件上传根目录
		$upload->autoSub = false;
		$upload->savePath = ''; // 设置附件上传（子）目录
		$upload->saveName = array('uniqid', '');
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			alertMes($upload->getError(), U('article/articleManage'));
		}else{// 上传成功
			$image = new \Think\Image();
			$imgAdd = 'App/artImg/' . $info['artImg']['savename'];
			$image->open($imgAdd);
			$bigPath = 'App/artImg/' . 'big' . md5(time() . $info['file']['savename']) . '.jpeg';
			$smallPath = 'App/artImg/' . 'small' . md5(time() . $info['file']['savename']) . '.jpeg';
			$image->thumb(1110, 370, \Think\Image::IMAGE_THUMB_FIXED)->save($bigPath);
			$image->thumb(238, 158, \Think\Image::IMAGE_THUMB_FIXED)->save($smallPath);
			unlink($imgAdd);
			$Path['bigPath'] = $bigPath;
			$Path['smallPath'] = $smallPath;
		}
		return $Path;
	}
}